/**
 * Copyright 2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flest.model
{
/**
 * @author		Ivan Petrov
 * @date		23/07/2007
 * @version		2.0
 * @purpose		Model handler, a wrapper-singleton around an
 * 				instance of HashMap.
 */
	import com.ericfeminella.utils.HashMap;
	import org.flest.constant.Errors;
	
	//[Bindable]
	public class ModelLocator
	{		
		private static var instance:ModelLocator = null;		
		private var values:HashMap;
		
		public function ModelLocator(singletonEnforcer:SingletonEnforcer) 
		{
			this.values = new HashMap();
		}
		
		public static function getInstance():ModelLocator 
		{
			if(instance == null) 
			{
				ModelLocator.instance = new ModelLocator(new SingletonEnforcer());
			}
			return ModelLocator.instance;
		}
		
		public function addValue(key:String, valueObj:Object):void
		{
			if(this.values.containsKey(key)) {
				throw new Error(Errors.MSG_DUPLICATE_MODEL_KEY);
			} else {
				this.values.put(key, valueObj);
			}			
		}
		
		public function getValue(key:String):Object
		{
			return this.values.getValue(key);
		}
		
		public function refresh():void
		{
			//TODO: remove null objects from the HashTable
		}
		
		public function removeValue(key:String):void
		{
			this.values.remove(key);
		}
		
		public function containsKey(key:String):Boolean
		{
			return this.values.containsKey(key);
		}
		
		public function containsValue(value:*):Boolean
		{
			return this.values.containsValue(value);
		}
		
		public function isEmpty():Boolean
		{
			return this.values.isEmpty(); 
		}
		
		public function updateValue(key:String, valueObj:Object):void
		{
			if(this.containsKey(key))
			{
				this.removeValue(key);
			}
			this.addValue(key, valueObj);
		}
	}
}
class SingletonEnforcer {}